﻿using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Query;
using isky.Plugin;

namespace isky.plugin
{
    public class CreatePostYGCBPlugin : IPlugin
    {


        /// <summary>
        /// 报价利润创建触发事件
        /// </summary>
        /// <param name="serviceProvider"></param>
        public void Execute(IServiceProvider serviceProvider)
        {
            SalesOrderYGCBSql salesOrderYGCBSql = new SalesOrderYGCBSql();
            // Obtain the execution context service from the service provider.
            var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            // Obtain the Organization Service factory service from the service provider
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            var service = factory.CreateOrganizationService(context.UserId);
            try
            {
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity inputEntity = context.InputParameters["Target"] as Entity;
                    if (inputEntity.Id==Guid.Empty)
                    {
                        return;
                    }
                    if (inputEntity.Contains("new_usdcost") && inputEntity.GetAttributeDefaultValue<EntityReference>("new_case", null) != null)
                    {
                        Entity orderentity = new Entity("salesorder", inputEntity.GetAttributeDefaultValue<EntityReference>("new_case", null).Id);
                        decimal totalus= salesOrderYGCBSql.GetYGCBInfo(service, inputEntity.GetAttributeDefaultValue<EntityReference>("new_case", null).Id, inputEntity.Id,"Create");
                        orderentity.Attributes["new_totalprofit"] = totalus;
                        if (totalus == 0)
                        {
                            orderentity.Attributes["new_totalprofitenglish"] = "ZERO";
                            orderentity.Attributes["new_totalprofitcapital"] = "零元整";
                        }
                        else
                        {
                            string totalusEnglish = salesOrderYGCBSql.NumberToString((double)totalus);
                            orderentity.Attributes["new_totalprofitenglish"] = totalusEnglish.ToUpper();

                            string totalusToUppe = salesOrderYGCBSql.MoneyToUpper(totalus.ToString());
                            orderentity.Attributes["new_totalprofitcapital"] = totalusToUppe;
                        }
                        Entity entity = service.Retrieve("salesorder", inputEntity.GetAttributeDefaultValue<EntityReference>("new_case", null).Id, new Microsoft.Xrm.Sdk.Query.ColumnSet("new_time"));
                        DateTime dateTime = entity.GetAttributeDefaultValue<DateTime>("new_time", DateTime.MinValue);
                        if (dateTime != DateTime.MinValue)
                        {
                            orderentity.Attributes["new_shipmentdate"] = dateTime.GetDateTimeFormats('D')[0].ToString();
                        }
                        service.Update(orderentity);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
                throw new NotImplementedException();
            }
        }




      
    }
}
